package longma.achai.test;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.jayway.android.robotium.solo.Solo;

import longma.achai.JSONTestActivity;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.Smoke;

public class DoubanApi extends ActivityInstrumentationTestCase2<JSONTestActivity> {
	public DoubanApi(){
		super("longma.achai", JSONTestActivity.class);
	}
	
	
	private Solo jsonSolo;
	private JSONTestActivity getJson;
	
	@Override
	protected void setUp() throws Exception {
		// TODO Auto-generated method stub
		super.setUp();
		jsonSolo = new Solo(getInstrumentation(), getActivity());
		getJson = getActivity();
		
		
		
	}
	

	@Smoke
	public void testGetJson() throws JSONException {
	//	jsonSolo.clickOnButton("获取JSON");
		//jsonSolo.sleep(5000);
		
		//测试UI222
		
		//String json = jsonSolo.getText(0).getText().toString();
		String json = getActivity().getJson();
		JSONObject doubanBook = new JSONObject(json);
		//2,获取json对象
		String cate = doubanBook.getString("category");
		JSONObject category = new JSONObject(doubanBook.getString("category"));
		String scheme = category.getString("@scheme");
		String term = category.getString("@term");
		
		//3,获取json 数组
		JSONArray dbTags = doubanBook.getJSONArray("db:tag");
		//List<JSONArray> tags = new ArrayList<JSON>();
	//	List<JSONArray> tags = new ArrayList<JSONArray>();
		//Map<String,String> showTags = new HashMap<String, String>();
		for(int i=0; i < dbTags.length(); i++){
			JSONObject tag = dbTags.getJSONObject(i);
			String name = tag.getString("@name");
			String count = tag.getString("@count");
		}
		assertEquals("http://www.douban.com/2007#kind", scheme);
		
		
	}
	
	
	@Override
	protected void tearDown() throws Exception {
		// TODO Auto-generated method stub
		super.tearDown();
	}
}
